﻿//------------------------------------------------------------------------------
//  
//------------------------------------------------------------------------------
#region Imported Namespaces
using System;
using System.Data;
using System.Configuration;
using System.Collections;

using SSRS;
using PHSRAG.Insight21.Reports;
using PHSRAG.Insight21.InsightUtilities;
using Utils = PHSRAG.Insight21.InsightUtilities;
using PHSRAG.Insight21.SearchContext;
using JournalEntryDetails = PHSRAG.Insight21.Purchasing.JournalEntryDetails;
//using UserContext = PHSRAG.Insight21.Context.UserContext;
//using AgreementsConstants = PHSRAG.Insight21.InsightUtilities.Constants.AgreementsConstants;

#endregion

namespace PHSRAG.Insight21.Agreements.Reports
{


    public class sqlFinancialTransactionReport : sqlReport
    {
        //private const string JournalIDKey = "JournalIDKey_534a7656-74ac-46b9-88c7-408eac9e1d46";
        //private const string StartDateKey = "StartDateKey_534a7656-74ac-46b9-88c7-408eac9e1d46";
        //private const string EndDateKey = "EndDateKey_534a7656-74ac-46b9-88c7-408eac9e1d46";
        //private const string FundNumberKey = "FundNumber_534a7656-74ac-46b9-88c7-408eac9e1d46";
        //private const string AccountIDKey = "AccountID_534a7656-74ac-46b9-88c7-408eac9e1d46";



        public sqlFinancialTransactionReport(string pageUrl)
            : base(pageUrl)
        {

        }

        public override string ReportName
        {
            get
            {
                return rptFormat=="Excel"?"Agmt_FinancialTransactionExcel":"Agmt_FinancialTransaction";
            }

        }

        public override ParameterValue[] setParameters(ref string rptName)
        {
            reportTitle = "Journal Entry";
            rptName = ReportName;
            JournalEntryDetails journalEntry = JournalEntry;
            reportSource = journalEntry.GetDataContainer();

            reportCriteria = loadCriteria(searchCriteria);

            return base.setParameters(ref rptName);
        }


        protected override string loadCriteria(SearchCriteria searchCriteria)
        {
            DateTime GLAsofDate = (DateTime)((DataSet)cachePolicy.GetCachedObject(RefreshDatesControlKey)).Tables[0].Rows[0]["glPostAsOfDate"];
            return string.Format(
                "Last GL Post|{0};"
                , GLAsofDate.ToShortDateString()
            );
        }

        private JournalEntryDetails JournalEntry
        {
            get
            {
                JournalEntryDetails journalEntryDetails =
               Policy.ResultsCache.GetObject(cachePolicy, JournalEntryDetails.CacheKey) as JournalEntryDetails;
                if (journalEntryDetails == null || journalEntryDetails.JournalID != JournalID)
                {
                    journalEntryDetails = new JournalEntryDetails();

                    if (userContext == null)
                        throw new Exception("Unable to retrieve the UserContext from cache");
                    journalEntryDetails.Load(
                        Utility.DataAccess.GetConnectionString(),
                        userContext.DomainUserId,
                        JournalID, StartDate, EndDate
                        , FundNumber
                        , AccountNumber);
                    
                    Policy.ResultsCache.SetObject(cachePolicy, JournalEntryDetails.CacheKey, journalEntryDetails);
                }
                return journalEntryDetails;
            }
        }

        private string JournalID
        {
            get
            {
                Hashtable incomingJournalData = pageCachePolicy.GetCachedObject(Keys.JournalEntryPageKey) as Hashtable;
                return (incomingJournalData == null || incomingJournalData[Keys.JournalIDKey] == null) ? string.Empty :
                    (string)incomingJournalData[Keys.JournalIDKey];
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.JournalEntryPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[Keys.JournalIDKey] = value;
                pageCachePolicy.Cache(Keys.JournalEntryPageKey, h);

            }

        }

        private string StartDate
        {
            get
            {
                Hashtable incomingJournalData = pageCachePolicy.GetCachedObject(Keys.JournalEntryPageKey) as Hashtable;
                return (incomingJournalData == null || incomingJournalData[Keys.JournalStartDateKey] == null) ? string.Empty :
                    (string)incomingJournalData[Keys.JournalStartDateKey];
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.JournalEntryPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[Keys.JournalStartDateKey] = value;
                pageCachePolicy.Cache(Keys.JournalEntryPageKey, h);

            }

        }

        private string EndDate
        {
            get
            {
                Hashtable incomingJournalData = pageCachePolicy.GetCachedObject(Keys.JournalEntryPageKey) as Hashtable;
                return (incomingJournalData == null || incomingJournalData[Keys.JournalEndDateKey] == null) ? string.Empty :
                    (string)incomingJournalData[Keys.JournalEndDateKey];
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.JournalEntryPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[Keys.JournalEndDateKey] = value;
                pageCachePolicy.Cache(Keys.JournalEntryPageKey, h);

            }

        }
        /// <summary>
        /// Gets the Journal Id passed from the orginating page and cache it using pagecache policy
        /// </summary>
        private string FundNumber
        {
            get
            {
                Hashtable incomingJournalData = pageCachePolicy.GetCachedObject(Keys.JournalEntryPageKey) as Hashtable;
                return (incomingJournalData == null || incomingJournalData[Keys.FundNumberKey] == null) ? string.Empty :
                    (string)incomingJournalData[Keys.FundNumberKey];
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.JournalEntryPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[Keys.FundNumberKey] = value;
                pageCachePolicy.Cache(Keys.JournalEntryPageKey, h);

            }

        }

        /// <summary>
        /// Gets the Journal Id passed from the orginating page and cache it using pagecache policy
        /// </summary>
        private string AccountNumber
        {
            get
            {
                Hashtable incomingJournalData = pageCachePolicy.GetCachedObject(Keys.JournalEntryPageKey) as Hashtable;
                return (incomingJournalData == null || incomingJournalData[Keys.AccountNumberKey] == null) ? string.Empty :
                    (string)incomingJournalData[Keys.AccountNumberKey];
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.JournalEntryPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[Keys.AccountNumberKey] = value;
                pageCachePolicy.Cache(Keys.JournalEntryPageKey, h);

            }

        }

    }
}
